<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//列表页控制器
class ListController extends Controller{
	
//	列表页
	public function index(){
//		分类
		$cateModel = new \Common\Model\Cate;
//		文章
		$arcModel = new \Common\Model\Arc;
//		标签
		$tagModel = new \Common\Model\Tag;
//		中间表
		$arcTagModel = new \Common\Model\ArcTag;
//		点击分类
		if($cid =Q('get.cid',0,'intval')){
			$name ='分类';
			$values = $cateModel->where("cid={$cid}")->pluck('cname');
	//		p($value);
	//		获得所有的子集
			$cids = $cateModel->getSon($cateModel->get(),$cid);
			$cids[] = $cid;	
			//查询文章表统计总数
			$total = $arcModel->where("category_cid IN (" . implode(',', $cids) . ")")->count();
			//查询分类对应的文章
			$arcData = $arcModel->join('category','category_cid','=','cid')->where("category_cid IN (" . implode(',', $cids) . ")")->get();
	//		p($arcData);
			foreach ($arcData as $key => $value) {
				$arcData[$key]['tag'] = $tagModel
						->join('article_tag','tid','=','tag_tid')
						->where("{$value['aid']}=article_aid")
						->get();
			}
		}
		
//		点击标签
		if($tid =Q('get.tid',0,'intval')){
			$name = '标签';
			$values = $tagModel->where("tid={$tid}")->pluck('tname');
			$total = $tagModel
					->join('article_tag','tid','=','tag_tid')
					->where("tid={$tid}")
					->count();
//			p($total);

//			文章下面的标签
			$arcData = $arcTagModel
					->join('article','aid','=','article_aid')
					->join('category','cid','=','category_cid')
					->where("tag_tid={$tid}")
					->get();
//			标签
			foreach ($arcData as $key => $value) {
				$arcData[$key]['tag'] = $tagModel
						->join('article_tag','tid','=','tag_tid')
						->where("{$value['aid']}=article_aid")
						->get();
			}
			
			
			
		}
		
//		p($arcData);
		View::with('name',$name);
		View::with('value',$values);
		View::with('total',$total);
		View::with('arcData',$arcData);
	    View::make();
	}
	
	
}